/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;


/**
 *
 * @author Fantasy2114
 */

public class paquete_mod_eli extends javax.swing.JFrame {
    Connection base;
    DefaultTableModel dtm;

    
    public paquete_mod_eli() {
         initComponents();
        try { 
           new local_connection();
            base=local_connection.getconnect();
        PreparedStatement tes = (PreparedStatement)base.prepareStatement("select id_paquete, nombre, descripcion, precio from paquetes");
        ResultSet cone = tes.executeQuery();
        int linea = 0;
       
        dtm = (DefaultTableModel) tabla_paq.getModel();
        dtm.setRowCount(0);
                 
            ArrayList<Object[]> data = new ArrayList<>();
          while (cone.next()) {
                Object[] rows = new Object[tabla_paq.getColumnCount()];
                for (int i = 0; i < rows.length; i++) {
                    rows[i] = cone.getObject(i + 1);
                }
                data.add(rows);
            }
            for (int i = 0; i < data.size(); i++) {
                dtm.addRow(data.get(i));
        }
       }
        
 catch (SQLException ex) {
            Logger.getLogger(Paquetes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void cargar () {
    try { 
           new local_connection();
            base=local_connection.getconnect();
        PreparedStatement tes = (PreparedStatement)base.prepareStatement("select id_paquete, nombre, descripcion, precio from paquetes");
        ResultSet cone = tes.executeQuery();
        int linea = 0;
       
        dtm = (DefaultTableModel) tabla_paq.getModel();
        dtm.setRowCount(0);
                 
            ArrayList<Object[]> data = new ArrayList<>();
          while (cone.next()) {
                Object[] rows = new Object[tabla_paq.getColumnCount()];
                for (int i = 0; i < rows.length; i++) {
                    rows[i] = cone.getObject(i + 1);
                }
                data.add(rows);
            }
            for (int i = 0; i < data.size(); i++) {
                dtm.addRow(data.get(i));
        }
       }
        
 catch (SQLException ex) {
            Logger.getLogger(Paquetes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_paq = new javax.swing.JTable();
        btm_aceptar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        box_buscar = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabla_paq.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre Paquete", "Descripción", "Precio"
            }
        ));
        jScrollPane1.setViewportView(tabla_paq);

        btm_aceptar.setText("Aceptar");
        btm_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btm_aceptarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Buscar Paquete");

        box_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                box_buscarActionPerformed(evt);
            }
        });
        box_buscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                box_buscarKeyReleased(evt);
            }
        });

        jButton1.setText("Modificar Paquete");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Eliminar Paquete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Crear Paquete");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Actualizar");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(box_buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btm_aceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE))
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(box_buscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btm_aceptar)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btm_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btm_aceptarActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btm_aceptarActionPerformed

    private void box_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_box_buscarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_box_buscarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        
        int row = tabla_paq.getSelectedRow();
        if (row != -1){
            try { 
               
                PreparedStatement tes = (PreparedStatement)base.prepareStatement("delete from paquetes where id_paquete = ?" );
                tes.setString(1,String.valueOf(tabla_paq.getValueAt(row,0)));
                tes.execute();
                dtm.removeRow(row);
                JOptionPane.showMessageDialog(this,"El paquete a sido eliminado");
                
                }
                catch (SQLException ex) {
                Logger.getLogger(Paquetes.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
        else {
        JOptionPane.showMessageDialog(this, "No a seleccionado ningun paquete");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        
        Paquetes abrir= new Paquetes(-1,this);
        abrir.setVisible (true);
        
    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
       
        int row = tabla_paq.getSelectedRow();
       if (row != -1){
        int mensaje = (int)tabla_paq.getValueAt(row, 0);
            Paquetes abrir = new Paquetes(mensaje,this);
        abrir.setVisible (true);
        
       }
       else {
        JOptionPane.showMessageDialog(this, "No a seleccionado ningun paquete");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void box_buscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_box_buscarKeyReleased

                     dtm.setRowCount(0);
            String select;
            String consulta;
            String como, where, nombre = "%";
            como = box_buscar.getText();
            StringTokenizer tokens = new StringTokenizer(como);
            while (tokens.hasMoreElements()) {
                nombre += (tokens.nextToken() + "%");
            }
            select = "select id_paquete, nombre, descripcion, precio from paquetes where ";
            where = "nombre like '" + nombre + "' or descripcion like '" + nombre + "' or id_paquete like '%" + como + "%' ";
            consulta = select + where;
            try {               
                PreparedStatement tes = (PreparedStatement)base.prepareStatement(consulta);
                ResultSet cone = tes.executeQuery();
                dtm.setRowCount(0);
                 
                ArrayList<Object[]> data = new ArrayList<>();
                while (cone.next()) {
                    Object[] rows = new Object[tabla_paq.getColumnCount()];
                    for (int i = 0; i < rows.length; i++) {
                        rows[i] = cone.getObject(i + 1);
                    }
                data.add(rows);
            }
        for (int i = 0; i < data.size(); i++) {
                dtm.addRow(data.get(i));        
        }
       }
        
 catch (SQLException ex) {
            Logger.getLogger(Paquetes.class.getName()).log(Level.SEVERE, null, ex);
        }  
    }//GEN-LAST:event_box_buscarKeyReleased

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
           dtm.setRowCount(0);
        try { 
           
            base=(Connection) local_connection.getconnect();
        PreparedStatement tes = (PreparedStatement)base.prepareStatement("select id_paquete, nombre, descripcion, precio from paquetes");
        ResultSet cone = tes.executeQuery();
        int linea = 0;
       
        dtm = (DefaultTableModel) tabla_paq.getModel();
        dtm.setRowCount(0);
                 
            ArrayList<Object[]> data = new ArrayList<>();
          while (cone.next()) {
                Object[] rows = new Object[tabla_paq.getColumnCount()];
                for (int i = 0; i < rows.length; i++) {
                    rows[i] = cone.getObject(i + 1);
                }
                data.add(rows);
            }
            for (int i = 0; i < data.size(); i++) {
                dtm.addRow(data.get(i));
            }
        }       
        catch (SQLException ex) {
            Logger.getLogger(Paquetes.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }//GEN-LAST:event_jButton4MouseClicked

    /**paque
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(paquete_mod_eli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(paquete_mod_eli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(paquete_mod_eli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(paquete_mod_eli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new paquete_mod_eli().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField box_buscar;
    private javax.swing.JButton btm_aceptar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla_paq;
    // End of variables declaration//GEN-END:variables
}
